package org.openwebtop.sunshine.action.user;

import org.openwebtop.sunshine.constants.GlobalConstants;
import org.openwebtop.sunshine.constants.GlobalForwards;
import org.openwebtop.sunshine.model.GlobalMessage;
import org.openwebtop.sunshine.user.UserContext;
import org.openwebtop.sunshine.user.UserContextAware;
import org.openwebtop.sunshine.util.GlobalContext;

public class LogoutAction implements UserContextAware {
	private UserContext userContext;
	private GlobalMessage globalMessage;

	public String execute() throws Exception {
		if (userContext.isLogin() == false) {
			this.globalMessage = new GlobalMessage();
			globalMessage.setTitle("You were not logged in.");
			globalMessage.setMessage("not logged in.");

		} else {
			this.globalMessage = new GlobalMessage();
			globalMessage.setTitle("You have been logged out.");
			globalMessage.setMessage("log out");

			GlobalContext.setAttribute(GlobalConstants.USER_CONTEXT_KEY, null);
		}

		return GlobalForwards.GLOBAL_MESSAGE;
	}

	@Override
	public void setUserContext(UserContext userContext) {
		this.userContext = userContext;
	}

	public GlobalMessage getGlobalMessage() {
		return globalMessage;
	}

}
